// 版本控制 Add 20250603
// let versionStatic= 2025062303;
let versionStatic= 2025063001;

reloadMain();
checkLogin();
loadMenu();
$(document).ready(function () {

    var user_name = $.cookie('u_name');
    $("#u_name").html(' ' + user_name + ' ');

    $(".logout").on('click', function () {
        logout();
    });

    $("#edit_password").on('click', function () {
        $("#modal_edit_password").modal('show');
    });

    $('#modal_edit_password').on('hide.bs.modal', function () {
        $("#txtOldPass").val('');
        $("#txtNewPass").val('');
        $("#txtRePass").val('');
        $('#fm_edit_password').bootstrapValidator('resetForm', true);
    });

    $(window).bind('hashchange', function() {
        window.location.reload();
    });

    $('#fm_edit_password').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            txtOldPass: {
                validators: {
                    notEmpty: {
                        message: '此项必填'
                    }
                }
            },
            txtNewPass: {
                validators: {
                    notEmpty: {
                        message: '此项必填'
                    }
                }
            },
            txtRePass: {
                validators: {
                    notEmpty: {
                        message: '此项必填'
                    },
                    callback: {
                        message: '两次密码不一致',
                        callback: function (value, validator) {
                            var new_pass = $('#txtNewPass').val();
                            return value == new_pass;
                        }
                    }
                }
            }
        }
    }).on('success.form.bv', function (e) {
        e.preventDefault();
        edit_password();
    });
});

/**
 * 1、回显菜单
 * 2、刷新iframe地址
 */
function reloadMain() {
    var hash = window.location.hash;
    if(hash && hash.length > 0) {
        var items = hash.substr(1, hash.length).split('#');
        if(items.length == 2) {
            var main_url = items[0];
            var m_id = items[1];
            $.cookie("m_id", m_id, {path:"/"});
            $("#main_iframe").attr('src',main_url+'#'+m_id);
        }
    }else{
        $.cookie('m_id', '', {path: "/", expires: -1});
    }
}

/**
 * 检查登录
 */
function checkLogin() {
    $("#index_loading").show();
    let is_login = $.cookie("is_login");  // 判断是否登录
    let is_token = $.cookie('token');     // 判断token

    console.log('checkLogin', is_login)
    if (is_login != "true") {
        location.href = "login.html";
    } else {
        $("#index_loading").hide();
        $("#wrapper").show();
    }
}

/**
 * 加载菜单
 */
function loadMenu() {
    var is_login = $.cookie("is_login");
    if (is_login != "true") {
        return;
    }
    var uid = $.cookie("u_id");
    var token = $.cookie("token");
    $.ajax({
        url: '/user/getMenus',
        type: "post",
        dataType: 'json',
        async: false,    //同步
        data: {
            parameters: JSON.stringify({
                common: {},
                data: {
                    id: uid,
                    token:token,
                }
            })
        },
        success: function (data) {
            var sel_menu_id =  $.cookie('m_id') || -9999;
            var third_menu = false;
            if (data.code == 1) {
                let menus = data.data;
                // 判断用户
                if (menus.length != 0) {
                    var length = menus.length;
                    var menu_html = '';
                    for (var i = 0; i < length; i++) {
                        //支持三级菜单
                        var menu = menus[i];
                        if (menu.menu_path && menu.menu_path.length > 0 && (!menu.children || menu.children.length == 0)) {
                            menu_html += "<li><a class=\"J_menuItem\" href=\"" + menu.menu_path+"?v=" + versionStatic +"#"+ menu.id + "\" data-menu-id=\"" + menu.id + "\" ><i class=\"fa fa-" + menu.icon_cls + "\"></i> <span class=\"nav-label\">" + menu.menu_name + "</span></a></li>";
                        } else {
                            menu_html += "" +
                                "<li>" +
                                "     <a href=\"#\"><i class=\"fa fa-" + menu.icon_cls + "\"></i> <span class=\"nav-label\">" + menu.menu_name + "</span><span class=\"fa arrow\"></span></a>" +
                                "     <ul class=\"nav nav-second-level\">";
                            for (var n = 0; n < menu.children.length; n++) {
                                var children = menu.children[n];
                                if (children.menu_path && children.menu_path.length > 0 && (!children.children || children.children.length == 0)) {
                                    menu_html += "<li><a class=\"J_menuItem\" href=\"" + children.menu_path+"?v=" + versionStatic+"#"+ children.id +  "\" data-menu-id=\"" + children.id + "\">" + children.menu_name + "</a></li>";
                                } else {
                                    menu_html += "<li><a href=\"#\">" + children.menu_name + "<span class=\"fa arrow\"></span></a>" +
                                        "<ul class=\"nav nav-third-level\">";
                                    if (children.children && children.children.length > 0) {
                                        for (var m = 0; m < children.children.length; m++) {
                                            var c = children.children[m];
                                            menu_html += "<li><a class=\"J_menuItem\" href=\"" + c.menu_path+"?v=" + versionStatic+"#"+ c.id + "\" data-menu-id=\"" + c.id + "\">" + c.menu_name + "</a></li>";
                                            if(sel_menu_id == c.id) {
                                                third_menu = true;
                                            }
                                        }
                                    }
                                    menu_html += "" +
                                        "    </ul>" +
                                        " </li>";
                                }
                            }
                            menu_html += "" +
                                "    </ul>" +
                                " </li>";
                        }
                    }
                    $("#side-menu").append(menu_html);

                    var sel_menu_id =  $.cookie('m_id') || -9999;
                    if (sel_menu_id > -9999) {
                        $("a.J_menuItem[data-menu-id="+sel_menu_id+"]").css('color','#FFF');
                        $("a.J_menuItem[data-menu-id=" + sel_menu_id + "]").parent().parent().parent().addClass('active');
                        if(third_menu) {
                            $("a.J_menuItem[data-menu-id=" + sel_menu_id + "]").parent().parent().parent().parent().parent().addClass('active');
                        }
                    }
                } else {
                    // 加载菜单为空 自动跳转到登录页面
                    $.cookie('is_login', '', {path: "/", expires: -1});
                    $.cookie('u_id', '', {path: "/", expires: -1});
                    $.cookie('u_name', '', {path: "/", expires: -1});
                    $.cookie('m_id', '', {path: "/", expires: -1});
                    $.cookie('token', '', {path: "/", expires: -1});
                    location.href = "login.html";
                }
            }
        }
    });
}

/**
 * 退出登录
 */
function logout() {
    $.dialog.confirm("提示", "确定要退出吗？", function () {
        $.cookie('is_login', '', {path: "/", expires: -1});
        $.cookie('u_id', '', {path: "/", expires: -1});
        $.cookie('u_name', '', {path: "/", expires: -1});
        $.cookie('m_id', '', {path: "/", expires: -1});
        $.cookie('token', '', {path: "/", expires: -1});
        location.href = "login.html";
    });
}

/**
 * 修改密码
 */
function edit_password() {
    var uid = $.cookie("u_id");
    $.ajax({
        url: '/user/updatePwd',
        type: "post",
        dataType: 'json',
        data: {
            parameters: JSON.stringify({
                common: {},
                data: {
                    id: uid,
                    password: $("#txtOldPass").val(),
                    newPass: $("#txtNewPass").val()
                }
            })
        },
        success: function (data) {
            if (data.code == 1) {
                $.cookie("is_login", "", {path: "/", expires: -1});
                $.cookie('u_id', '', {path: "/", expires: -1});
                $.cookie('u_name', '', {path: "/", expires: -1});
                $.cookie('m_id', '', {path: "/", expires: -1});
                $.dialog.alert("提示", "密码修改成功，请重新登录", "success", function () {
                    location.href = "login.html";
                });
            } else {
                $.dialog.alert("提示", data.msg, "error");
            }
            $("#modal_edit_password").modal('hide');
        },
        error: function (data) {
            $.dialog.alert("提示", data.msg, "error");
        }
    });
}